package com.sgcc.platforminterface;

import android.content.Context;
import android.os.RemoteException;
import android.text.TextUtils;


import com.sgcc.pda.data.entity.PushOfflineMsg;

import com.sgcc.pda.hzaidl.push.PushReceiver;
import com.sgcc.pda.tools.JsonUtils;
import com.sgcc.pda.tools.LogPrintUtils;



import java.util.List;

/**
 * @创建者：suven on 2019/9/4 13:26
 * @描述：推送消息
 * @版本：1.0
 * @修改人：
 * @修改时间：
 * @修改说明：
 * @修改版本：
 */
public class PushMsgManager {

    private static PushMsgManager mInstance;

    public static PushMsgManager getInstance() {
        if (mInstance == null) {
            mInstance = new PushMsgManager();
        }
        return mInstance;
    }

    //初始工单类型
    public void subscribe(PushReceiver.PushReceiverCallBack pushReceiverCallBack, Context mContext, String packageName, String... topic) {
        LogPrintUtils.d(String.format("mqtt-subscribe:%s", packageName));
        try {
            new PushReceiver(mContext, packageName, topic).setPushReceiverCallBack(pushReceiverCallBack);
        } catch (Exception e) {
            e.printStackTrace();
            LogPrintUtils.d(String.format("mqtt-subscribe:%s", packageName),e);
        }
    }

    /**
     * 离线消息
     * packageName  包名
     * 返回： [{"content":"c1","timeTag":"1","topic":"t1"},{"content":"c2","timeTag":"2","topic":"t2"}]
     */
    public List<PushOfflineMsg> getOfflineMsg(String packageName) {
        try {
            String result = HttpManager.getOfflineMsg(packageName);
            if (!TextUtils.isEmpty(result)) {
                List<PushOfflineMsg> list = JsonUtils.toList(result, PushOfflineMsg.class);
                if (list != null) {
                    //上报
                    for (PushOfflineMsg pushOfflineMsg : list) {
                        if (pushOfflineMsg != null && !TextUtils.isEmpty(pushOfflineMsg.getTimeTag())) {
                            boolean bool = reportReceivedMsg(packageName, pushOfflineMsg.getTimeTag());
                            LogPrintUtils.d(String.format("mqtt:上报结果：%s  %s", bool, pushOfflineMsg.getTimeTag()));
                        } else {
                            LogPrintUtils.d("mqtt-getOfflineMsg为空");
                        }
                    }
                    return list;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            LogPrintUtils.d(String.format("mqtt-getOfflineMsg异常:%s", packageName),e);
        }
        return null;
    }

    /**
     * 上报已收到消息
     * packageName  包名
     * time 时间戳tag
     */

    public boolean reportReceivedMsg(String packageName, String timestamp) {
        try {
            LogPrintUtils.d(String.format("mqtt-reportReceivedMsg packageName：%s  ；timestamp：%s", packageName, timestamp));
            return HttpManager.reportReceivedMsg(packageName, String.valueOf(timestamp));
        } catch (RemoteException e) {
            e.printStackTrace();
            LogPrintUtils.d(String.format("mqtt-reportReceivedMsg异常：%s  ；timestamp：%s", packageName, timestamp),e);
        }
        return false;
    }

    /**
     * 发送推送消息
     *
     * @param topic
     */
    public boolean publish(String topic, String content) {
        try {
            LogPrintUtils.d(String.format("mqtt-publish发送主题：%s  ；内容：%s", topic, content));
            return HttpManager.publish(topic, content);
        } catch (RemoteException e) {
            e.printStackTrace();
            LogPrintUtils.d(String.format("mqtt-publish发送异常：%s  ；内容：%s", topic, content),e);
        }
        return false;
    }

    /**
     * 解绑推送
     *
     * @param topic
     */
    public void unSubscribe(String packageName, String... topic) {
        for (int i = 0; i < topic.length; i++) {
            try {
                HttpManager.unsubscribe(packageName, topic[i]);
                LogPrintUtils.d(String.format("mqtt-unSubscribe取消%s", topic[i]));
            } catch (RemoteException e) {
                e.printStackTrace();
                LogPrintUtils.d(String.format("mqtt-unSubscribe取消异常%s", topic[i]),e);
            }
        }
    }

}
